var app = getApp();
Component({
  properties: {
    params: {
      type: null
    },
    index: {
      type: null
    },
    data: {
      type: null
    },
    designer: {
      type: null
    },
    sessionurl: {
      type: null
    },
    Identification: {
      type: null
    },
    grouid: {
      type: null
    },
    total: {
      type: null
    },
    bottomShow: {
      type: null
    },
    isBottomnum: {
      type: null
    }
  },
  data: {
    language: '',
    showMore: true,
    page: 1,
    total_page: 0,
    isLoadMore: true
  }, // 私有数据，可用于模板渲染

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      let language = wx.getStorageSync('langIndex');
      this.setData({ 'language': language.en});
      this.triggerEvent('isLoadMorebtn', this.data.isLoadMore);
      let nowTime = new Date().getTime();
      for (let i = 0; i < this.data.data.length; i++) {
        let start_time = this.data.data[i].start_time;
        let beginTime = new Date(start_time).getTime();
        this.setData({
          ['data[' + i + '].day']: '00',
          ['data[' + i + '].hou']: '00',
          ['data[' + i + '].min']: '00',
          ['data[' + i + '].sec']: '00',
        });
        if (nowTime > beginTime) {
          this._countDown(i, this.data.data[i].end_time);
          this.setData({
            ['data[' + i + '].begShow']: true
          });
        } else {
          this._countDown(i, this.data.data[i].start_time);
          this.setData({
            ['data[' + i + '].begShow']: false
          });
        }
      }
    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {

  }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {

  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    getMoreData() {
      let that = this;
      if (that.data.page >= that.data.total_page) {
        return;
      } else {
        that.setData({
          page: that.data.page + 1
        });
        let urlStr = app.getNetAddresss('home-page.get-page-goods');
        urlStr += '&page=' + that.data.page;
        urlStr += '&group_id=' + that.data.grouid;
        app._getNetWork({
          url: urlStr,
          success: function(resdata) {
            var res = resdata.data;
            if (res.result == 1) {
              let data = that.data.data;
              that.setData({
                data: data.concat(res.data.data)
              });
              if (that.data.page >= that.data.total_page) {
                that.setData({
                  isLoadMore: false
                });
                that.triggerEvent('isLoadMorebtn',  that.data.isLoadMore);
              }
            } else {
              that.setData({
                page: that.data.page - 1,
                isLoadMore: false
              });
              that.triggerEvent('isLoadMorebtn',  that.data.isLoadMore);
            }
          },
          fail: function(res) {
            console.log(res);
          }
        });
      }
    },
    showTime(time) {
      let nowTime = new Date().getSeconds;
      let beginTime = new Date(time).getSeconds;
      if (nowTime >= beginTime) {
        return true;
      } else {
        return false;
      }
    },
    //倒计时
    _countDown: function(index, endTime_1) {
      let newTime = new Date().getTime();
      let endTime = new Date(endTime_1).getTime();
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        this.setData({
          ['data[' + index + '].day']: this._timeFormat(day),
          ['data[' + index + '].hou']: this._timeFormat(hou),
          ['data[' + index + '].min']: this._timeFormat(min),
          ['data[' + index + '].sec']: this._timeFormat(sec),
        });
      } else { //活动已结束，全部设置为'00'
        this.setData({
          ['data[' + index + '].day']: '00',
          ['data[' + index + '].hou']: '00',
          ['data[' + index + '].min']: '00',
          ['data[' + index + '].sec']: '00',
        });
      }
      if (endTime - newTime < 0) {
        this.setData({
          ['data[' + index + '].day']: '00',
          ['data[' + index + '].hou']: '00',
          ['data[' + index + '].min']: '00',
          ['data[' + index + '].sec']: '00',
        });
      } else {
        setTimeout(this._countDown.bind(this, index, endTime_1), 1000);
      }
    },
    //小于10的格式化函数
    _timeFormat(param) { //小于10的格式化函数
      return param < 10 ? '0' + param : param;
    },
    buy(e) {
      let item = e.currentTarget.dataset.item;
      wx.navigateTo({
        url: '/packageA/detail_v2/detail_v2?id=' + item.goodid
      });
    },
  },
  observers: {
    'isBottomnum': function(num) {
      console.log(num);
      this.getMoreData();
    },
    'params': function() {
      this.setData({
        showMore: true,
        page: 1,
        total_page: 0,
        isLoadMore: true
      });
      this.triggerEvent('isLoadMorebtn', this.data.isLoadMore);
      this.setData({
        total_page: Math.ceil(this.data.total / 12)
      });
      if (this.data.page >= this.data.total_page) {
        this.setData({
          isLoadMore: false
        });
        this.triggerEvent('isLoadMorebtn',  this.data.isLoadMore);
      }
    }
  }
});
